package top.techqi.usbd.desc

class IADDesc(
    val bFirstInterface: Byte,
    val bInterfaceCount: Byte,
    val bFunctionClass: Byte,
    val bFunctionSubClass: Byte,
    val bFunctionProtocol: Byte,
    val iFunction: Byte,

    val functionName: String?,
) : AbstractDesc(LENGTH, TYPE) {
    constructor(
        bytes: ByteArray,
        functionName: String?,
    ) : this(
        bytes[2],
        bytes[3],
        bytes[4],
        bytes[5],
        bytes[6],
        bytes[7],

        functionName,
    ) {
        require(bytes[1] == TYPE) {
            "Invalid type: ${bytes[1]}"
        }
    }

    override fun toString(): String {
        return """
        IADDesc(
            bFirstInterface     = %1${'$'}s,
            bInterfaceCount     = %2${'$'}s,
            bFunctionClass      = %3${'$'}s,
            bFunctionSubClass   = %4${'$'}s,
            bFunctionProtocol   = %5${'$'}s,
            iFunction           = %6${'$'}s
        )
        """.trimIndent().format(
            bFirstInterface,
            bInterfaceCount,
            bFunctionClass,
            bFunctionSubClass,
            bFunctionProtocol,
            "%1$-2d(%2\$s)".format(iFunction, functionName)
        )
    }

    companion object {
        const val TYPE: Byte = 0x0B
        const val LENGTH: Byte = 8
    }
}
